package com.zenithmind.document.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;

/**
 * 文档访问日志创建DTO
 * 遵循单一职责原则：专注于访问日志创建数据传输
 * 遵循开闭原则：通过验证注解支持扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档访问日志创建DTO")
public class DocumentAccessLogCreateDTO {

    @Schema(description = "文档ID", required = true, example = "doc123456")
    @NotBlank(message = "文档ID不能为空")
    private String documentId;

    @Schema(description = "用户ID", required = true, example = "user123456")
    @NotBlank(message = "用户ID不能为空")
    private String userId;

    @Schema(description = "访问类型", required = true, example = "VIEW", allowableValues = {"VIEW", "DOWNLOAD", "SHARE", "EDIT", "DELETE"})
    @NotBlank(message = "访问类型不能为空")
    private String accessType;

    @Schema(description = "用户代理", example = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
    private String userAgent;

    @Schema(description = "IP地址", example = "192.168.1.100")
    private String ipAddress;

    @Schema(description = "访问来源", example = "WEB", allowableValues = {"WEB", "MOBILE", "API", "DESKTOP"})
    private String accessSource;

    @Schema(description = "设备类型", example = "PC", allowableValues = {"PC", "MOBILE", "TABLET", "OTHER"})
    private String deviceType;

    @Schema(description = "浏览器类型", example = "Chrome")
    private String browserType;

    @Schema(description = "操作系统", example = "Windows 10")
    private String operatingSystem;

    @Schema(description = "屏幕分辨率", example = "1920x1080")
    private String screenResolution;

    @Schema(description = "访问时长（秒）", example = "120")
    private Long accessDuration;

    @Schema(description = "页面停留时间（秒）", example = "60")
    private Long stayDuration;

    @Schema(description = "引用页面", example = "https://example.com/documents")
    private String referrerUrl;

    @Schema(description = "访问路径", example = "/document/view/123")
    private String accessPath;

    @Schema(description = "查询参数", example = "?tab=content&page=1")
    private String queryParams;

    @Schema(description = "会话ID", example = "session123456")
    private String sessionId;

    @Schema(description = "地理位置", example = "北京市朝阳区")
    private String location;

    @Schema(description = "经度", example = "116.4074")
    private Double longitude;

    @Schema(description = "纬度", example = "39.9042")
    private Double latitude;

    @Schema(description = "网络类型", example = "WiFi", allowableValues = {"WiFi", "4G", "5G", "3G", "2G", "Ethernet", "Unknown"})
    private String networkType;

    @Schema(description = "访问状态", example = "SUCCESS", allowableValues = {"SUCCESS", "FAILED", "TIMEOUT", "UNAUTHORIZED"})
    private String accessStatus;

    @Schema(description = "错误代码", example = "404")
    private String errorCode;

    @Schema(description = "错误信息", example = "文档不存在")
    private String errorMessage;

    @Schema(description = "响应时间（毫秒）", example = "150")
    private Long responseTime;

    @Schema(description = "数据传输量（字节）", example = "1024000")
    private Long dataTransferred;

    @Schema(description = "访问时间", example = "2025-01-09T10:30:00")
    private LocalDateTime accessTime;

    @Schema(description = "扩展属性（JSON格式）", example = "{\"feature\": \"preview\", \"version\": \"1.0\"}")
    private String extendedProperties;

    @Schema(description = "备注", example = "用户通过搜索访问")
    private String remarks;

    /**
     * 创建查看日志DTO
     */
    public static DocumentAccessLogCreateDTO createViewLog(String documentId, String userId, String userAgent, String ipAddress) {
        DocumentAccessLogCreateDTO dto = new DocumentAccessLogCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setAccessType("VIEW");
        dto.setUserAgent(userAgent);
        dto.setIpAddress(ipAddress);
        dto.setAccessTime(LocalDateTime.now());
        dto.setAccessStatus("SUCCESS");
        return dto;
    }

    /**
     * 创建下载日志DTO
     */
    public static DocumentAccessLogCreateDTO createDownloadLog(String documentId, String userId, String userAgent, String ipAddress) {
        DocumentAccessLogCreateDTO dto = new DocumentAccessLogCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setAccessType("DOWNLOAD");
        dto.setUserAgent(userAgent);
        dto.setIpAddress(ipAddress);
        dto.setAccessTime(LocalDateTime.now());
        dto.setAccessStatus("SUCCESS");
        return dto;
    }

    /**
     * 创建分享日志DTO
     */
    public static DocumentAccessLogCreateDTO createShareLog(String documentId, String userId, String shareType, String userAgent, String ipAddress) {
        DocumentAccessLogCreateDTO dto = new DocumentAccessLogCreateDTO();
        dto.setDocumentId(documentId);
        dto.setUserId(userId);
        dto.setAccessType("SHARE");
        dto.setUserAgent(userAgent);
        dto.setIpAddress(ipAddress);
        dto.setAccessTime(LocalDateTime.now());
        dto.setAccessStatus("SUCCESS");
        dto.setExtendedProperties("{\"shareType\": \"" + shareType + "\"}");
        return dto;
    }

    /**
     * 验证DTO数据
     */
    public boolean isValid() {
        return documentId != null && !documentId.trim().isEmpty() &&
               userId != null && !userId.trim().isEmpty() &&
               accessType != null && !accessType.trim().isEmpty();
    }

    /**
     * 获取访问类型枚举
     */
    public enum AccessType {
        VIEW("查看"),
        DOWNLOAD("下载"),
        SHARE("分享"),
        EDIT("编辑"),
        DELETE("删除"),
        UPLOAD("上传"),
        COPY("复制"),
        MOVE("移动"),
        RENAME("重命名"),
        COMMENT("评论");

        private final String description;

        AccessType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 获取访问来源枚举
     */
    public enum AccessSource {
        WEB("网页端"),
        MOBILE("移动端"),
        API("API接口"),
        DESKTOP("桌面端"),
        MINI_PROGRAM("小程序"),
        H5("H5页面");

        private final String description;

        AccessSource(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 获取设备类型枚举
     */
    public enum DeviceType {
        PC("个人电脑"),
        MOBILE("手机"),
        TABLET("平板"),
        TV("智能电视"),
        WATCH("智能手表"),
        OTHER("其他设备");

        private final String description;

        DeviceType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
